﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;


namespace KD.Sms.Monitor
{
    public partial class frmRunState : Form
    {
        bool _bIsBusy = false;
        public frmRunState()
        {
            InitializeComponent();
        }

        private void frmRunState_Load(object sender, EventArgs e)
        {
            Task.Cyj.Instance.PlcDataReadOut += OnPlcDataReadOut;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="send"></param>
        /// <param name="e"></param>
        private void OnPlcDataReadOut(object send, Task.ActiveReadEventArgs e)
        {
            if (_bIsBusy)
                return;
            try
            {
                short[] _arrReads = (short[])e.E_ReadValue;
                UI.FormHelper.SetControlText(labDcdq, _arrReads[0 / 2].ToString());
                UI.FormHelper.SetControlText(labXcdq, _arrReads[2 / 2].ToString());
                UI.FormHelper.SetControlText(labSjdq, _arrReads[6 / 2].ToString());
                UI.FormHelper.SetControlText(labDcmb, _arrReads[16 / 2].ToString());
                UI.FormHelper.SetControlText(labXcmb, _arrReads[18 / 2].ToString());
                UI.FormHelper.SetControlText(labSjmb, _arrReads[20 / 2].ToString());
                UI.FormHelper.SetControlText(labCyds, _arrReads[14 / 2].ToString());
                UI.FormHelper.SetControlText(labDqth, _arrReads[10 / 2].ToString());
                UI.FormHelper.SetControlText(labSffk, _arrReads[22 / 2].ToString());
                //保存当前缩分值
                CacheSystemControl.E_ReductioncCurrent = (int)_arrReads[22 / 2];

                int[] _aryBits330 = DataConvert.Ushort2Binary((int)(_arrReads[30 / 2]));
                if (_aryBits330[0] == 1)
                    UI.FormHelper.SetControlText(labLcfk, "暂停");
                else if (_aryBits330[1] == 1)
                    UI.FormHelper.SetControlText(labLcfk, "采样复位流程");
                else if (_aryBits330[2] == 1)
                    UI.FormHelper.SetControlText(labLcfk, "手动中");
                else if (_aryBits330[3] == 1)
                    UI.FormHelper.SetControlText(labLcfk, "采样自动准备好");
                //else if (_aryBits330[4] == 1)
                //    UI.FormHelper.SetControlText(labLcfk, "车厢检测流程");
                else if (_aryBits330[5] == 1)
                    UI.FormHelper.SetControlText(labLcfk, "去目标点流程");
                else if (_aryBits330[6] == 1)
                    UI.FormHelper.SetControlText(labLcfk, "下降流程");
                else if (_aryBits330[7] == 1)
                    UI.FormHelper.SetControlText(labLcfk, "下降到位等待");
                else if (_aryBits330[8] == 1)
                    UI.FormHelper.SetControlText(labLcfk, "下降到位等待");
                else if (_aryBits330[9] == 1)
                    UI.FormHelper.SetControlText(labLcfk, "回卸料点流程");
                //else if (_aryBits330[10] == 1)
                //    UI.FormHelper.SetControlText(labLcfk, "自动接料");
                else if (_aryBits330[11] == 1)
                    UI.FormHelper.SetControlText(labLcfk, "集料斗开");
                else if (_aryBits330[12] == 1)
                    UI.FormHelper.SetControlText(labLcfk, "制样流程");
                //else if (_aryBits330[13] == 1)
                //    UI.FormHelper.SetControlText(labLcfk, "自动摆开");
                else
                    UI.FormHelper.SetControlText(labLcfk, "无流程");
            }
            catch (Exception ex)
            {
                FormHelper.ApplicationShow(UI.MsgType.Error, "工况解析出错!详见日志!");
                Log.LogHelper.DoWriteLogTypeSubitem(Log.LogType.Error, Log.LogSubitem.Forms, "工况解析出错!");
                Log.LogHelper.DoWriteLogTypeSubitem(Log.LogType.Error, Log.LogSubitem.Forms, ex.ToString());
            }
            finally
            {
                _bIsBusy = false;
            }
        }

        private void cbVoice_SelectedIndexChanged(object sender, EventArgs e)
        {
            Thread thread = new Thread(new ParameterizedThreadStart(DoSound)) { IsBackground = true };
            thread.Start(cbVoice.Text);
        }

        private void btnSend_Click(object sender, EventArgs e)
        {
            Thread thread = new Thread(new ParameterizedThreadStart(DoSound)) { IsBackground = true };
            thread.Start(cbVoice.Text);
        }


        /// <summary>
        /// 播报语音
        /// </summary>
        /// <param name="_strVoiceStr"></param>
        private void DoSound(object _strVoiceStr)
        {
            UI.FormHelper.SetControlEnabled(btnSend, false);
            UI.FormHelper.SetControlEnabled(cbVoice, false);
            Voicer.Instance.DoSendSound("," + _strVoiceStr.ToString());
            System.Threading.Thread.Sleep(3000);
            UI.FormHelper.SetControlEnabled(btnSend, true);
            UI.FormHelper.SetControlEnabled(cbVoice, true);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSet_Click(object sender, EventArgs e)
        {
            frmCommonConfigLone obj = new frmCommonConfigLone("缩分间隔设置");
            obj.ShowDialog();
            if (obj.DialogResult == DialogResult.OK)
            {
                if (!Task.Cyj.Instance.DoWrite(154, obj.E_Value))
                    UI.MsgBox.Show(UI.MsgType.Error, "操作失败");
                else
                    DAL.SqlLite.Main.Instance.LogWrite(SysLogType.Login, string.Format("采样缩分设定{0}成功", obj.E_Value));
            }
        }



    }
}
